<?php
// +----------------------------------------------------------------------
// | Etion [ http://www.etion.cn ]
// +----------------------------------------------------------------------
// | File: FuserAction.class.php
// +----------------------------------------------------------------------
// | Date: 上午08:40:55
// +----------------------------------------------------------------------
// | Author: 李汝康 <37739419@qq.com>
// +----------------------------------------------------------------------
// | 文件描述: 前台会员模块
// +----------------------------------------------------------------------

/**
 * @name前台会员模块
 *
 */
class FuserAction extends FbaseAction {
	
	/**
	 * 
	 * @nameUer实例
	 * @var object
	 */
	protected $_mUser = '';
	
	/**
	 * @name初始化
	 * @see FbaseAction::_initialize()
	 */
	protected function _initialize() {
		parent::_initialize();
		$this->_mUser = D ('User','admin');				
	}
	
	/**
	 * @name登录后会员中心主页
	 * 
	 */	
	public function home() {
		$in = &$this->in;
		if (empty($_SESSION['userinfo'])) { //如果没有登录，就跳转
			$this->message(L ('您还没有登录，请先登录！'), __ROOT__ . '/index.html?m=findex&a=login');
		}
		$data = $this->_mUser->getUserData($_SESSION['userinfo']['user_id'],true);
		$data['user_id']=$_SESSION['userinfo']['user_id'];
		$this->assign('data',$data);
		$seo['seotitle'] = L ('会员面板') . C('SITE_TITLE_SEPARATOR') . L('会员中心') . C('SITE_TITLE_SEPARATOR') . C('SEOTITLE');
		$seo['seokeywords'] = C('SEOKEYWORDS');
		$seo['seodescription'] = C('SEODESCRIPTION');
	    $seo['url'] = C ('SITEURL') . 'user/home';
	    $this->assign('seo',$seo);
		$this->display('user/home.html');
	}
	
	/**
	 * @name编辑个人资料、包括修改密码
	 * 
	 */
	public function edit() {
		$in = &$this->in;
		if (empty($_SESSION['userinfo'])) { //如果没有登录，就跳转
			$this->message(L ('您还没有登录，请先登录！'), __ROOT__ . '/index.html?m=findex&a=login');
		}
		//前台用户修改资料
		if ($this->ispost()) {
			unset($in['info']['username']);
			import('User',INCLUDE_PATH);
			$_user = get_instance_of('User');	
			if (! $_user->autoCheckToken ( $in ))
				$this->message ( L ('<font class="red">请不要非法提交或者重复提交页面！</font>') );	
			//前台更新，过滤不可以修改的信息
			//$in['info']['user_id'] = $_SESSION['userinfo']['user_id'];
			unset($in['info']['role_id']);// 角色信息
			unset($in['info']['username']); // 用户名
			unset($in['info']['last_login_ip']); //
			unset($in['info']['last_login_time']); //
			unset($in['info']['create_time']); //
			unset($in['info']['active']); //
			unset($in['info']['login_count']); //
			unset($in['info']['status']); //
			unset($in['info']['isadmin']); //
			
			$where=$in['info']['user_id'];
			if (false !== $_user->update($in['info'])) {
				$sql="update `fangfa_user_person` set `sex`=".$in['info']['sex']." where `user_id`=".$where;
				mysql_query($sql);
				$this->message(L ('保存成功！'), __ROOT__ . '/index.html?m=fuser&a=edit');
			} else {
				$this->message(L ('保存失败！') . $_user->getError(), __ROOT__ . '/index.html?m=fuser&a=edit');
			}
		}
		//获取用户详细信息
		$data = $this->_mUser->getUserData($_SESSION['userinfo']['user_id'],true);
		$data['user_id']=$_SESSION['userinfo']['user_id'];
		$this->assign('data',$data);
		$seo['seotitle'] = L ('会员面板') . C('SITE_TITLE_SEPARATOR') . L('会员中心') . C('SITE_TITLE_SEPARATOR') . C('SEOTITLE');
		$seo['seokeywords'] = C('SEOKEYWORDS');
		$seo['seodescription'] = C('SEODESCRIPTION');
	    $seo['url'] = C ('SITEURL') . 'user/edit';
		$this->assign('seo',$seo);
		$this->display('user/home.html');
	}
	
	/**
	 * @name用户忘记密码
	 * 
	 */
	public function register() {		
		$in = &$this->in;
		if (!C('USER_OPEN_REGISTER')) { // 检查是否开启前台用户注册
			$this->message('系统暂未开放用户注册功能！');
		}
		if ($in['ajax']) $this->_ajax_register();
		if ($this->ispost()) {
			$in['info']['role_id'] = $role_id = C('USER_DEFAULT_ROLE_ID');  //赋予用户默认角色
			import('User', INCLUDE_PATH);
			$_user = get_instance_of('User');
			$count = $_user->where("username='".$in['info']['username']."'") -> count();
			if ($count > 0) {
				 $this->message("该用户名已被占用，请重新输入！",__ROOT__."/index.html?m=findex&a=register");
				 exit();
			}
//			if (!preg_match('/\w+@\w+(\.\w+)+/', $in['info']['email'])) {
//				$this->message('您输入的邮箱地址格式不正确！', __ROOT__."/index.html?m=findex&a=register");
//			}
			if ($_SESSION['verify'] != md5($in['verify'])) {
				$this->message('验证码输入错误！', __ROOT__."/index.html?m=findex&a=register");
			}
			if (!$_user->autoCheckToken($in)) 
				$this->message(L('请不要非法提交或者重复提交页面！'), __ROOT__ . '/index.html?m=findex&a=register'); 
			if (C('USER_NEED_ACTIVE')) $in['info']['status'] = '0';  //置为未激活状态
			else $in['info']['status'] = '1';  //无需验证，直接激活
			$result = $_user->register($in['info'],$role_id,0);
			if (true === $result) {
				if (C('USER_NEED_ACTIVE')) { //需要激活					
					$this->message( L('下一步，到邮箱激活即可！'), __ROOT__);
				} else {  //不需要激活		
					$_SESSION['userinfo']="";			
					$this->message( L('恭喜您，注册成功！'), __ROOT__ . '/index.html?m=findex&a=login');
				}
			} else {
				$this->message( L('注册失败！') . $_user->getError());
			}
		}
		$url = array_search(array('Findex','register'), C('_routes_'));
		!empty($url) && $seo['url'] = C ('SITEURL') . '/' . $url;
		$seo['seotitle'] = L('用户注册') . C('SITE_TITLE_SEPARATOR') .C('SEOTITLE');
		$seo['seokeywords'] = C('SEOKEYWORDS');
		$seo['seodescription'] = C('SEODESCRIPTION');
		$this->assign('forward',$seo['url']);
		$this->assign('seo',$seo);		
		$this->display('user/register.html');
	}
	
	/**
	 * @name注销登录
	 */
	public function logout() {
		if (isset ( $_SESSION ['userinfo'] )) {
//			import ( 'Act', INCLUDE_PATH );
//			$_act = get_instance_of ( 'Act' );
//			$_act->clearUser ();
			$_SESSION['userinfo']="";
			$this->assign ( "forward", __ROOT__ . '/index.html?m=findex&a=login' );
			$this->message ( L ( '退出成功' ),  __ROOT__ . '/index.html?m=findex&a=login');
		} else {
			$this->message ( L ( '已经退出' ) );
		}
	}
	
	/**
	 * @name修改密码
	 */
	
	public function password() {
		$in = &$this->in;
		if (empty($_SESSION['userinfo'])) { //如果没有登录，就跳转
			$this->message(L ('您还没有登录，请先登录！'), __ROOT__ . '/index.html?m=findex&a=login');
		}
		if($this->ispost()){
			$_user = D ('User');
			$where['password']=md5($in['info']['oldpassword']);
			$where['user_id']=$in['info']['user_id'];
			$num=$_user->where($where)->find();
			if($num){
				$whe['user_id']=$in['info']['user_id'];
				$da['password']=md5($in['info']['newpassword']);
				$_user->where($whe)->save($da);
				$_SESSION['userinfo']="";
				$this->message(L ('密码修改成功，请重新登录！'), __ROOT__ . '/index.html?m=findex&a=login');
			}else{
				$this->message(L ('原密码不正确！'), __ROOT__ . '/index.html?m=fuser&a=password');
			}
		}
		$data = $this->_mUser->getUserData($_SESSION['userinfo']['user_id'],true);
		$this->assign('data',$data);
		$seo['seotitle'] = L ('会员密码修改') . C('SITE_TITLE_SEPARATOR') . L('会员中心') . C('SITE_TITLE_SEPARATOR') . C('SEOTITLE');
		$seo['seokeywords'] = C('SEOKEYWORDS');
		$seo['seodescription'] = C('SEODESCRIPTION');
	    $seo['url'] = C ('SITEURL') . 'user/home';
	    $this->assign('seo',$seo);
		$this->display('user/password.html');
	}

}